local daohai = fk.CreateSkill {
  name = "lingling__daohai",
}

Fk:loadTranslationTable{
  ["lingling__daohai"] = "倒海",
  [":lingling__daohai"] = "每轮限一次，其他角色额定弃牌阶段开始时，你可以与其各执行一个额外的摸牌阶段，若其该弃牌阶段未弃置牌，你失去此技能。",

  ["#lingling__daohai-invoke"] = "倒海：是否与 %dest 各执行一个摸牌阶段？若其弃牌阶段未弃牌则你失去本技能",
}

daohai:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target ~= player and player:hasSkill(daohai.name) and target.phase == Player.Discard and
      not target.dead and player:usedSkillTimes(daohai.name, Player.HistoryRound) == 0 and data.reason == "game_rule"
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player, {
      skill_name = daohai.name,
      prompt = "#lingling__daohai-invoke::"..target.id,
    }) then
      event:setCostData(self, {tos = {target}})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player:gainAnExtraPhase(Player.Draw, daohai.name, false)
    if not target.dead then
      target:gainAnExtraPhase(Player.Draw, daohai.name, false)
    end
  end,
})

daohai:addEffect(fk.EventPhaseEnd, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target.phase == Player.Discard and
      player:usedSkillTimes(daohai.name, Player.HistoryTurn) > 0 and
      #player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if move.from == target and move.moveReason == fk.ReasonDiscard then
            return true
          end
        end
      end, Player.HistoryPhase) == 0
  end,
  on_use = function(self, event, target, player, data)
    player.room:handleAddLoseSkills(player, "-lingling__daohai")
  end,
})

return daohai
